package core.controller.cmds;

import core.controller.CommandController;
import core.dal.AdminFunctionality;
import exceptions.UserNotFoundException;

public class DeleteUserCMD implements ICmd{

	AdminFunctionality functionality;
	final String level = "Admin";
	ICmd nextCMD;
	
	CommandController cc;
	
	public DeleteUserCMD(CommandController commandController, AdminFunctionality aF){
		this.cc = commandController;
		functionality = aF;
	}
	
	@Override
	public void setNextCmd(ICmd nextCmd) {
		nextCMD = nextCmd;
		
	}

	@Override
	public void execute(String cmd) {
		if (cmd.equals("delUser")) {
			int id;

			cc.getTUI().showOutput("Enter ID to delete: ");
			id = Integer.parseInt(cc.getTUI().getInput());

				
					try {
						
						//compares the current user with the user deleted in the database
						
						if (functionality.showUser(id).getOprId()==cc.getCurrentUser().getOprId()){
							cc.getTUI().showOutput("YOU CAN'T DELETE YOURSELF");
							}
						
						else{
							
						functionality.deleteUser(id); 
						cc.getTUI().showOutput("USER DELETED");
						}
						
					} catch (UserNotFoundException e) {
						cc.getTUI().showOutput(e.getMessage());
					} 
				}
			
			
			

		

		if (nextCMD != null) {
			nextCMD.execute(cmd);
		
			
		
		}
	}

	@Override
	public String getInfo() {
		return "delUser - Delete a user";
		
	}

	@Override
	public String getLevel() {
		return this.level;
		
	}
}
